import 'package:sports_new/entity/live/expert_room_entity.dart';
import 'package:sports_new/entity/live/live_gift_entity.dart';
import 'package:sports_new/entity/live/live_goods_entity.dart';
import 'package:sports_new/entity/live/live_login_entity.dart';
import 'package:sports_new/entity/live/live_statistics_entity.dart';
import 'package:sports_new/server/ws/ali_im_manager.dart';
import 'package:sports_new/utils/ali_message.dart';

import '../../entity/expert/zq_hot_expert_entity.dart';
import '../../entity/live/live_detail_entity.dart';
import '../../entity/live/live_board_entity.dart';
import '../../entity/live/live_chat_entity.dart';
import '../../entity/live/live_chat_info.dart';
import '../../entity/match/expert_views_entity.dart';
import '../api_dir.dart';
import '../http/dio_utils.dart';

class LiveApi{
  //直播列表
  static Future<List<LiveDetailEntity>> getLiveList(int sportsId) async {
    final params = {"sportsId": sportsId};
    try {
      final result = await DioUtils.post("/${ApiDir.plan}/app-live/roomList",
          params: params);
      if (result.statusCode == 200 && result.data["c"] == 200) {
        List<LiveDetailEntity> data = result.data["d"]
            .map<LiveDetailEntity>((e) => LiveDetailEntity.fromJson(e))
            .toList();
        return data;
      }
    }catch(e){
      return [];
    }
    return [];
  }

  //直播详情
  static Future<LiveDetailEntity?> getLiveDetail(int? liveId) async {
    final params = {"id": liveId};
    final result = await DioUtils.post("/${ApiDir.plan}/app-live/room",
      params: params,
      showToast: false
    );
    if (result.statusCode == 200 && result.data["c"] == 200) {
      LiveDetailEntity data = LiveDetailEntity.fromJson(result.data["d"]);
      return data;
    }
    return null;
  }

  //获取聊天室历史
  static Future<List<LiveChatEntity>> getChatHistory(
      {int? chatId, int? id}) async {
    final result = await DioUtils.post(
      "/${ApiDir.plan}/app-live/chatList",
      params: {"chatId": chatId,"id": id}
    );
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<LiveChatEntity> data = result.data["d"].map<LiveChatEntity>(
        (e) => LiveChatEntity.fromJson(e)
      ).toList();
      return data;
    }
    return [];
  }

  //获取聊天室配置信息
  static Future<LiveChatInfo?> getChatInfo(int? id) async {
    final result = await DioUtils.post(
      "/${ApiDir.plan}/app-live/chatInfo",
      params: {"id": id}
    );
    if (result.statusCode == 200 && result.data["c"] == 200) {
      LiveChatInfo? data = LiveChatInfo.fromJson(result.data["d"]);
      return data;
    }
    return LiveChatInfo(gift: true,goods: true,plan: true);
  }

  //退出直播间
  static Future<bool?> closeLiveRoom(int? id) async {
    final result = await DioUtils.post(
      "/${ApiDir.plan}/app-live/outRoom", params: {"id": id}
    );
    if (result.statusCode == 200 && result.data["c"] == 200) {
      return result.data["d"];
    }
    return false;
  }
  //发送聊天
  static Future<LiveChatEntity?> sendChat(String? message, int? id) async {
    final result = await DioUtils.post(
      "/${ApiDir.plan}/app-live/chat",
      params: {"message": message,"id": id}
    );
    if (result.statusCode == 200 && result.data["c"] == 200) {
      AliImManager.recordAdd({"消息发送成功:message": message,"id": id}.toString());
      LiveChatEntity? data = LiveChatEntity.fromJson(result.data["d"]);
      return data;
    }
    AliImManager.recordAdd({"消息发送失败:message": message,"id": id}.toString());
    return null;
  }

  ///专家直播间列表
  static Future<List<ExpertRoomEntity>> getExpertRooms(
      int page, int size) async {
    final params = {
      "data": {},
      "page": page,
      "pageSize": size
    };
    final result = await DioUtils.post(
        "/${ApiDir.plan}/app-live/roomListByExpert",
        params: params);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<ExpertRoomEntity> data =
      result.data["d"]["rows"].map<ExpertRoomEntity>(
        (e) => ExpertRoomEntity.fromJson(e)
      ).toList();
      return data;
    }
    return [];
  }

  ///专家直播
  static Future<int?> startLive(int id) async {
    final params = {"id": id};
      final result = await DioUtils.post(
          "/${ApiDir.plan}/app-live/startLive", params: params);
    if (result.data != null) {
      return result.data["c"];
    }
    return null;
  }

  ///专家关闭直播
  static Future<int?> closeLive(int id) async {
    final result = await DioUtils.post(
        "/${ApiDir.plan}/app-live/forbidLive", params: id
    );
    if (result.data != null) {
      return result.data["c"];
    }
    return null;
  }

  //直播登录鉴权
  static Future<LiveLoginEntity?> loginLiveInfo() async {
    final result = await DioUtils.post("/${ApiDir.plan}/app-live-group/auth");
    try {
      if (result.statusCode == 200 && result.data["c"] == 200) {
        LiveLoginEntity? data = LiveLoginEntity.fromJson(result.data["d"]);
        return data;
      }
    }catch(e){
      return null;
    }
    return null;
  }

  ///查询用户禁言
  static Future<bool?> checkMuteUser(int roomId,String userId) async {
    final params = {
      "roomId": roomId,
      "userId": userId
    };
    try {
      final result = await DioUtils.post(
          "/${ApiDir.plan}/app-live/queryUserGagStatusOfGroup",
          params: params
      );
      if (result.data != null) {
        return result.data["d"];
      }
    }catch(e){
      return false;
    }
    return null;
  }

  ///用户禁言
  static Future<bool?> muteUser(int roomId,String userId,bool alreadyMute) async {
    final params = {
      "roomId": roomId,
      "userId": userId
    };
    try{
      final result = await DioUtils.post(
        alreadyMute?"/${ApiDir.plan}/app-live/liftRoomChatGag":
        "/${ApiDir.plan}/app-live/roomChatGag",
        params: params
      );
      if (result.data != null) {
        return result.data["d"];
      }
    }catch(e){
      return false;
    }
    return null;
  }

  ///删除消息
  static Future<bool?> deleteMessage(int messageId) async {
    try{
      final result = await DioUtils.post(
          "/${ApiDir.plan}/app-live/delRoomChat",
          params: messageId
      );
      if (result.data != null) {
        return result.data["d"];
      }
    }catch(e){
      return false;
    }
    return null;
  }

  //直播统计
  static Future<LiveStatisticsEntity> getStatistics(int? liveId) async {
    final result = await DioUtils.post(
      "/${ApiDir.plan}/app-live/queryLiveRoomStatisticsNew",
      params: liveId
    );
    try {
      if (result.statusCode == 200 && result.data["c"] == 200) {
        LiveStatisticsEntity? data = LiveStatisticsEntity.fromJson(result.data["d"]);
        return data;
      }
    }catch(e){
      return LiveStatisticsEntity();
    }
    return LiveStatisticsEntity();
  }

  //直播礼物
  static Future<List<GiftEntity>> getLiveGift(int? liveId) async {
    final result = await DioUtils.post(
      "/${ApiDir.plan}/app-gift-price-do/getGiftsListOfRoom",params: liveId
    );
    try {
      if (result.statusCode == 200 && result.data["c"] == 200) {
        List<GiftEntity> data = result.data["d"].map<GiftEntity>(
                (e) => GiftEntity.fromJson(e)
        ).toList();
        return data;
      }
    }catch(e){
      return [];
    }
    return [];
  }

  //直播榜单
  static Future<List<BoardEntity>> getLiveBoard(int? liveId) async {
    final result = await DioUtils.post(
        "/${ApiDir.plan}/app-gift-price-do/getTopConsumers",params: liveId
    );
    try {
      if (result.statusCode == 200 && result.data["c"] == 200) {
        List<BoardEntity> data = result.data["d"].map<BoardEntity>(
                (e) => BoardEntity.fromJson(e)
        ).toList();
        return data;
      }
    }catch(e){
      return [];
    }
    return [];
  }

  ///删除消息
  static Future<int?> sendGift(int roomId,int priceId,int count) async {
    try{
      final result = await DioUtils.post(
        "/${ApiDir.plan}/app-gift-price-do/giveGifts",
        params: {
          "count": count,
          "priceId": priceId,
          "roomId": roomId
        }
      );
      if (result.data != null) {
        return result.data["c"];
      }
    }catch(e){
      return null;
    }
    return null;
  }

  //创建直播
  static Future<LiveDetailEntity?> createLive(
    String title, String publishTime, String image,{String? message,int sportsId = 1}
  ) async {
    final params = {
      "image": image,
      "message": message,
      "publishTime": publishTime,
      "sportsId": sportsId,
      "title": title
    };
    try {
      final result = await DioUtils.post("/${ApiDir.plan}/app-live/roomAdd",
          params: params,showToast: true);
      if (result.statusCode == 200 && result.data["c"] == 200) {
        LiveDetailEntity data = LiveDetailEntity.fromJson(result.data["d"]);
        return data;
      }
    }catch(e){
      return null;
    }
    return null;
  }

  //直播商品
  static Future<List<GoodsEntity>> getLiveGoods(int? liveId) async {
    final result = await DioUtils.post(
      "/${ApiDir.plan}/app-live/queryLiveGoods",params: {"id": liveId}
    );
    try {
      if (result.statusCode == 200 && result.data["c"] == 200) {
        List<GoodsEntity> data = result.data["d"].map<GoodsEntity>(
          (e) => GoodsEntity.fromJson(e)
        ).toList();
        return data;
      }
    }catch(e){
      return [];
    }
    return [];
  }

  ///直播专家推荐
  static Future<List<HotExpertEntity>> getLiveExpertHot(int sportsId) async {
    final result =
    await DioUtils.post("/${ApiDir.plan}/app-expert-do/liveExpertTop", params: sportsId);
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<HotExpertEntity> data = result.data["d"]
        .map<HotExpertEntity>((e) => HotExpertEntity.fromSeparateValue(e))
        .toList();
      return data;
    }
    return [];
  }

  ///直播列表方案
  static Future<List<PlanEntity>> getLivePlans(int page, int size, int sportsId) async {
    final result = await DioUtils.post(
      "/${ApiDir.plan}/app-plan-do/lastPlanHotWithLiveArray",
      params: {
        "page": page,
        "pageSize": size,
        "data": {
          "sportsId": "$sportsId"
        }
      }
    );
    if (result.statusCode == 200 && result.data["c"] == 200) {
      List<PlanEntity> data = result.data["d"].map<PlanEntity>(
        (e) => PlanEntity.fromJson(e)
      ).toList();
      return data;
    }
    return [];
  }
}